package com.peng.leetcode.greedy;

import java.util.Map;
import java.util.TreeMap;

/**
 * CountGoodRectangles
 *
 * @author: lupeng6
 * @create: 2021/2/1 18:58
 */
public class CountGoodRectangles {

    public static void main(String[] args) {
        int[][] rectangles = {{5, 8}, {3, 9}, {5, 12}, {16, 5}};
        System.out.println(new CountGoodRectangles().countGoodRectangles(rectangles));
    }

    public int countGoodRectangles(int[][] rectangles) {
        TreeMap<Integer, Integer> edges = new TreeMap<>((a, b) -> b - a);
        for (int[] rectangle : rectangles) {
            int maxEdge = Math.min(rectangle[0], rectangle[1]);
            edges.putIfAbsent(maxEdge, 0);
            edges.computeIfPresent(maxEdge, (key, olbVal) -> olbVal + 1);
        }
        for (Map.Entry<Integer, Integer> entry : edges.entrySet()) {
            return entry.getValue();
        }
        return -1;
    }

}
